package com.example.iitematicviewer.api.login;

import com.alibaba.fastjson.JSONObject;
import com.example.iitematicviewer.constant.ExceptionConstant;
import com.example.iitematicviewer.mapper.LoginDao;
import com.example.iitematicviewer.util.RSAUtil;
import com.example.iitematicviewer.vo.ResultVO;
import com.example.iitematicviewer.vo.UserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class LoginService {

    @Autowired
    private LoginTransService service;

    @Autowired
    private LoginDao dao;

    /**
     * 登录新增方法
     * @param param
     * @param resultVO
     * @return
     */
    @Async
    public ResultVO save(JSONObject param,ResultVO resultVO){
        resultVO.setCode(ExceptionConstant.two);
        try {
            //判空方法
            resultVO = service.judgingEmptinessByLogin(param,resultVO);
            if (resultVO.getCode().equals(ExceptionConstant.five)) {
                return resultVO;
            }
            //是否已存在此用户(暂存，防止需返工)
            /*String userId = dao.selectOneByUser(param.getString(LoginConstant.user));
            if (StringUtils.isNotEmpty(userId)) {
                resultVO.setCode(ExceptionConstant.five);
                resultVO.setMessage(LoginExceptionConstant.badinsert);
                return resultVO;
            }*/
            //将JSON转换为登录实体
            UserVO userVO = service.transUserVOByJSON(param, resultVO);
            if (null != userVO){
                dao.insert(userVO);
            }
            resultVO.setMessage(ExceptionConstant.insert);
        }
        catch (Exception e){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badInsert + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 登录方法
     * @return
     */
    @Async
    public ResultVO login(JSONObject param,ResultVO resultVO){
        resultVO.setCode(ExceptionConstant.two);
        try {
            //登录判空方法
            resultVO = service.judgingEmptinessByLogin(param,resultVO);
            if (resultVO.getCode().equals(ExceptionConstant.five)) {
                return resultVO;
            }
            //登录方法
            UserVO userVO = dao.selectAllByUser(param.getString(LoginConstant.user));
            if (null == userVO){
                resultVO.setCode(ExceptionConstant.five);
                resultVO.setMessage(LoginExceptionConstant.badLoginByPassword);
                return resultVO;
            }
            //判断是否可冻结
            if (userVO.getBotNum() == 3){
                resultVO.setCode(ExceptionConstant.five);
                resultVO.setMessage(LoginExceptionConstant.freeze);
                return resultVO;
            }
            String password = userVO.getPassword();
            String newPassword = param.getString(LoginConstant.password);
            resultVO = service.encryption(userVO,resultVO, password,newPassword);
        }
        catch (Exception e){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(LoginExceptionConstant.badLogin + e.getMessage());
        }
        return resultVO;
    }

}
